import pandas as pd
import numpy as np
import ast

# 读取 CSV 文件
data_path = 'D:\\学习&科研\\华为手表项目\\华为数据\\base_data\\all_stages_df_info.csv'
data = pd.read_csv(data_path)

# 将 polar_hr 和 polar_rr 列转换为列表
data['polar_hr'] = data['polar_hr'].apply(ast.literal_eval)
data['polar_rr'] = data['polar_rr'].apply(ast.literal_eval)

# 定义函数来计算统计特征
def calculate_statistics(lst):
    return {
        'mean': np.mean(lst),
        'std': np.std(lst),
        'min': np.min(lst),
        'max': np.max(lst),
        'median': np.median(lst),
        'q1': np.percentile(lst, 25),
        'q3': np.percentile(lst, 75),
        'range': np.max(lst) - np.min(lst),
        'skewness': pd.Series(lst).skew(),
        'kurtosis': pd.Series(lst).kurtosis()
    }

# 计算 polar_hr 的统计特征
hr_stats = data['polar_hr'].apply(calculate_statistics).apply(pd.Series)
data = pd.concat([data, hr_stats.add_prefix('polar_hr_')], axis=1)

# 计算 polar_rr 的统计特征
rr_stats = data['polar_rr'].apply(calculate_statistics).apply(pd.Series)
data = pd.concat([data, rr_stats.add_prefix('polar_rr_')], axis=1)


# data.to_csv('D:\\学习&科研\\华为手表项目\\华为数据\\base_data\\all_stages_df_statistics.csv', index=False)



# 指定要计算的列
selected_columns = [	'psychology_RPE',	'physiology_RPE',		'la'	,	'speed',		'polar_hr_mean',	'polar_hr_std',
                    	'polar_hr_min',	'polar_hr_max',	'polar_hr_median'	,'polar_hr_q1',	'polar_hr_q3',	
                        'polar_hr_range',	'polar_hr_skewness',	'polar_hr_kurtosis',	'polar_rr_mean',	
                        'polar_rr_std',	'polar_rr_min'	,'polar_rr_max'	,'polar_rr_median',	
                        'polar_rr_q1',	'polar_rr_q3'	,'polar_rr_range',	'polar_rr_skewness',	'polar_rr_kurtosis',
                        'sex','age','hight','weight']
data['age'] = pd.to_numeric(data['age'], errors='coerce')
data['hight'] = pd.to_numeric(data['hight'], errors='coerce')
data['weight'] = pd.to_numeric(data['weight'], errors='coerce')

# 按 'number' 列分组并计算每组内的皮尔森相关系数
grouped_data = data.groupby('number')

# 存储每组的相关系数
correlation_results = []

# 计算每组的相关系数
for name, group in grouped_data:
    # 计算皮尔森相关系数
    correlations = group[selected_columns].corr()['physiology_RPE']
    # 将结果存储在列表中
    correlation_results.append(correlations)

# 将结果转换为 DataFrame 并计算平均值
correlation_df = pd.DataFrame(correlation_results)
average_correlations = correlation_df.mean()

# 打印结果
print("每组的指定统计特征与 physiology_RPE 的平均皮尔森相关系数：")
print(average_correlations)
